package com.kuaifawu.lwnlawyerclient.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kuaifawu.lwnlawyerclient.Adapter.LWNAdapter_home;
import com.kuaifawu.lwnlawyerclient.Adapter.LWNAdapter_messageCenter;
import com.kuaifawu.lwnlawyerclient.Adapter.LWNAdapter_messageSystem;
import com.kuaifawu.lwnlawyerclient.Lib.LWNJsonToData;
import com.kuaifawu.lwnlawyerclient.LWNNetwork.LWNNetworkCenter;
import com.kuaifawu.lwnlawyerclient.Lib.ToastView_custom;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_message;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_messageSystem;
import com.kuaifawu.lwnlawyerclient.Model.LWNModel_user;
import com.kuaifawu.lwnlawyerclient.R;
import com.kuaifawu.lwnlawyerclient.Lib.LWNSwip.LWSwipeRefreshLayout;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LWNMessageCenter extends Activity implements LWSwipeRefreshLayout.OnRefreshListener,LWSwipeRefreshLayout.OnLoadListener {
    @ViewInject(R.id.mc_back)
    private ImageButton button_back;
    @ViewInject(R.id.mc_clean)
    private Button button_clean;
    @ViewInject(R.id.mc_cursor)
    private ImageView cursor;// 动画图片
    @ViewInject(R.id.mc_wait)
    private TextView textView_dongtai;
    @ViewInject(R.id.mc_already)
    private TextView textView_xitong;// 页卡头标
    @ViewInject(R.id.mc_viewpager)
    private ViewPager pagerView_mc;

    private ListView listView_dongtai;
    private ListView listView_xitong;
    private LWNAdapter_messageCenter adapter_dongtai;
    private LWNAdapter_messageSystem adapter_xitong=null;

    private List<View> listViews; // Tab页面列表
    private int currIndex = 0;// 当前页卡编号
    private ArrayList<LWNModel_message> array_dongtai;
    private ArrayList<LWNModel_messageSystem> array_xitong;
    private ArrayList<LWNModel_message> array_Alldongtai;
    private ArrayList<LWNModel_messageSystem> array_Allxitong;
    private LWNMessageCenter activity_self;

    private LWSwipeRefreshLayout nulldata_refreshsystem;
    private LWSwipeRefreshLayout nulldata_refreshdongtai;


    private View view_dongtai;
    private View view_xitong;
    private int type=0;
    private String[] items = new String[] { "删除该消息" };
    int one;

    private LWSwipeRefreshLayout dongtai_data_refresh;
    private LWSwipeRefreshLayout xitong_data_refresh;
    private int start=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_center);
        activity_self = this;
        ViewUtils.inject(this);
        initMCView();
        InitViewPager();
        InitTextView();
        setRefresh();
        getDongtaiMessageFromNetwork();
    }

    private void initMCView(){
        array_Alldongtai=new ArrayList<LWNModel_message>();
        array_Allxitong=new ArrayList<LWNModel_messageSystem>();
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(20, intent);
                finish();
            }
        });

        button_clean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cleanUnreadFromNetwork(type);
            }
        });

        DisplayMetrics dm = new DisplayMetrics();
        activity_self.getWindowManager().getDefaultDisplay().getMetrics(dm);
        one = dm.widthPixels / 2;// 页卡1 -> 页卡2 偏移量

    };

    /**
     * 初始化头标
     */
    private void InitTextView() {
        textView_dongtai.setOnClickListener(new MyOnClickListener(0));
        textView_xitong.setOnClickListener(new MyOnClickListener(1));

    }

    /**
     * 初始化动画
     */
    private void InitImageView() {
        /*bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.image_scroll)
                .getWidth();// 获取图片宽度
        System.out.println("bmpW:" + bmpW);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        System.out.println("screenW:" + screenW);
        offset = screenW/2;// 计算偏移量*/
        Matrix matrix = new Matrix();
        matrix.postTranslate(0, 0);
        cursor.setImageMatrix(matrix);// 设置动画初始位置
    }

    /**
     * 头标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {

            if (index == 0) {
                type=0;
                textView_dongtai.setTextColor(getResources().getColor(R.color.theme_color));
                textView_xitong.setTextColor(getResources().getColor(R.color.color_item_content));

            } else {
                type=1;
                textView_xitong.setTextColor(getResources().getColor(R.color.theme_color));
                textView_dongtai.setTextColor(getResources().getColor(R.color.color_item_content));
            }
            pagerView_mc.setCurrentItem(index);
        }
    }


    private void setRefresh(){
        dongtai_data_refresh = (LWSwipeRefreshLayout) view_dongtai.findViewById(R.id.dongtai_data_refresh);
        dongtai_data_refresh.setColor(android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        dongtai_data_refresh.setMode(LWSwipeRefreshLayout.Mode.BOTH);
        dongtai_data_refresh.setLoadNoFull(false);
        dongtai_data_refresh.setOnRefreshListener(this);
        dongtai_data_refresh.setOnLoadListener(this);
        dongtai_data_refresh.setRefreshing(true);


        xitong_data_refresh = (LWSwipeRefreshLayout) view_xitong.findViewById(R.id.xitong_data_refresh);
        xitong_data_refresh.setColor(android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        xitong_data_refresh.setMode(LWSwipeRefreshLayout.Mode.BOTH);
        xitong_data_refresh.setLoadNoFull(false);
        xitong_data_refresh.setOnRefreshListener(this);
        xitong_data_refresh.setOnLoadListener(this);
      //  dongtai_data_refresh.setRefreshing(true);


        nulldata_refreshdongtai=(LWSwipeRefreshLayout) view_dongtai.findViewById(R.id.nulldata_refreshdongtai);
        nulldata_refreshdongtai.setColor(android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        nulldata_refreshdongtai.setMode(LWSwipeRefreshLayout.Mode.BOTH);
        nulldata_refreshdongtai.setLoadNoFull(false);
        nulldata_refreshdongtai.setOnRefreshListener(this);
        nulldata_refreshdongtai.setOnLoadListener(this);

        nulldata_refreshsystem = (LWSwipeRefreshLayout) view_xitong.findViewById(R.id.nulldata_refreshsystem);
        nulldata_refreshsystem.setColor(android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        nulldata_refreshsystem.setMode(LWSwipeRefreshLayout.Mode.BOTH);
        nulldata_refreshsystem.setLoadNoFull(false);
        nulldata_refreshsystem.setOnRefreshListener(this);
        nulldata_refreshsystem.setOnLoadListener(this);

    }





    @Override
    public void onLoad() {
        start=start+10;

        if(type==0){

            getDongtaiMessageFromNetwork();
        }else if(type==1){
            getSystemMessageFromNetwork();
        }

    }

    @Override
    public void onRefresh() {
        start=0;
        if(type==0){
            getDongtaiMessageFromNetwork();
        }else if(type==1){
            getSystemMessageFromNetwork();
        }
    }

    /**
     * 初始化ViewPager
     */
    private void InitViewPager() {
        listViews = new ArrayList<View>();
        view_dongtai = View.inflate(this, R.layout.view_mc_dongtai, null);
        view_xitong = View.inflate(this, R.layout.view_mc_xitong, null);

        listViews.add(view_dongtai);
        listViews.add(view_xitong);

        pagerView_mc.setAdapter(new LWNAdapter_home(listViews));
        pagerView_mc.setCurrentItem(0);
        pagerView_mc.setOnPageChangeListener(new MyOnPageChangeListener());

        //初始化等待回答列表
        listView_dongtai = (ListView) view_dongtai.findViewById(R.id.listView_dongtai);

        listView_dongtai.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               if(!isShow){
                   selectDongtaiItem(position);
                   }else{
                   return;
               }


            }
        });



        listView_xitong = (ListView) view_xitong.findViewById(R.id.listView_xitong);
        listView_xitong.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

               if(!isShow){
                   selectXiTongItem(position);
                        }else{
                return;
                   }

            }
        });
        setListClick();
    }


    /**
     * 长按删除
     */
      private void setListClick(){
          listView_dongtai.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
              @Override
              public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                  String title="";
                  if(array_Alldongtai.get(position).getFlag()==1){
                      title="用户评价消息";
                  }else if(array_Alldongtai.get(position).getFlag()==0){
                      title="用户追问消息";
                  }
                  showDialog(title,array_Alldongtai.get(position).getId());
                  return false;
              }
          });

          listView_xitong.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
              @Override
              public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                  showDialog(array_Allxitong.get(position).getTitle(),array_Allxitong.get(position).getId());
                  return false;
              }
          });


       }





    /**
     * 对话框
     * @param title
     */
    private boolean isShow =false;
    AlertDialog.Builder alertDialog=null;
    private void showDialog(String title, String msg_id){
        final String  id=msg_id;
        isShow=true;
        alertDialog  =new  AlertDialog.Builder(this);
        alertDialog.setTitle(title);
        alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                isShow=false;
            }
        });

        alertDialog.setItems(items,new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                deletMessage(id);
            }
        }).create().show();
//       new AlertDialog.Builder(this).setTitle(title).setItems(items,new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                switch (which){
//                    case 0:
//                        isShow=true;
//                        deletMessage(id);
//
//                }
//            }
//                }
//
//        ).create().show();


    }


    /**
     * 页卡切换监听
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = new TranslateAnimation(one * currIndex, one * arg0, 0, 0);
            cursor.startAnimation(animation);
            start=0;
            switch (arg0) {
                case 0: {
                    type=0;
                    getDongtaiMessageFromNetwork();
                    textView_dongtai.setTextColor(getResources().getColor(R.color.theme_color));
                    textView_xitong.setTextColor(getResources().getColor(R.color.color_item_content));

                }
                break;
                case 1: {
                    type=1;
                    getSystemMessageFromNetwork();
                    textView_xitong.setTextColor(getResources().getColor(R.color.theme_color));
                    textView_dongtai.setTextColor(getResources().getColor(R.color.color_item_content));

                }
                break;
            }

            currIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(300);
            cursor.startAnimation(animation);
        }


        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    }
    LWNNetworkCenter center = LWNNetworkCenter.getInstance();
    //String string_url = center.getInstance().uploadQuestionUnreadCount(message_id, activity_self);
    //点击列表
    private void selectDongtaiItem(int position){
        Intent intent = new Intent();
        LWNModel_message model_dongtai = array_Alldongtai.get(position);
        intent.putExtra("questionid", model_dongtai.getQuestion_id());
        intent.setClass(this, LWNQuestionDetailActivity.class);
        startActivityForResult(intent, 200);
        String string_url = center.getInstance().uploadQuestionUnreadCount(model_dongtai.getQuestion_id(), activity_self);
        uploadQuestionMessageFromNetwork(string_url);
    }


    /**
     * 判断点击系统通知消息
     * @param position
     */
    private void selectXiTongItem(int position){

        LWNModel_messageSystem item= array_Allxitong.get(position);
        String string_url = center.getInstance().uploadSystemUnreadCount(item.getId(), activity_self);
        uploadQuestionMessageFromNetwork(string_url);


        int automatic=Integer.parseInt(item.getAutomatic());
        String skipUrl="";
        if(automatic==1){//根据连接跳转
           skipUrl=item.getLink();
            Intent intent = new Intent();
            intent.setClass(this, LWNSkipWebViewActivity.class);
            Bundle bundle=new Bundle();
            bundle.putString("skipurl",skipUrl);
            intent.putExtras(bundle);
            startActivity(intent);
        }else if(automatic==2){//我的成长升级推送
            Intent intent = new Intent();
            intent.setClass(this, GrowMedalActivity.class);
            Bundle bundle=new Bundle();
            bundle.putInt("index",0);
            intent.putExtras(bundle);
           startActivity(intent);
           // finish();
        }else if(automatic==3){//成长页面勋章
            Intent intent = new Intent();
            intent.setClass(this, GrowMedalActivity.class);
            Bundle bundle=new Bundle();
            bundle.putInt("index",1);
            intent.putExtras(bundle);
            startActivity(intent);
           // finish();


        }

    }
     private void skipWeb(String url){
         Uri content_url = Uri.parse(url);
         Intent intentfeedback= new Intent();
         intentfeedback.setAction("android.intent.action.VIEW");
         PackageManager pm=getPackageManager();
         intentfeedback.setData(content_url);
         List<ResolveInfo> listInfo=pm.queryIntentActivities(intentfeedback,0);
         if(listInfo.size()>0) {
             startActivity(intentfeedback);
         }else{
             Toast.makeText(LWNMessageCenter.this,"请检查浏览器，稍后重试",Toast.LENGTH_LONG).show();
         }

     }

    //返回刷新列表
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 200: {
                start=0;
                getDongtaiMessageFromNetwork();
            }
            break;
            default: {

            }
            break;
        }
    }



   private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case 0001:
                dongtai_data_refresh.setVisibility(View.VISIBLE);
                nulldata_refreshdongtai.setVisibility(View.GONE);
                dongtai_data_refresh.setRefreshing(false);
                if (adapter_dongtai == null) {
                    adapter_dongtai = new LWNAdapter_messageCenter(view_dongtai.getContext(), R.layout.view_mc_dongtai, array_Alldongtai);
                    listView_dongtai.setAdapter(adapter_dongtai);
                } else {
                    adapter_dongtai.notifyDataSetChanged();
                }
                break;
            case 0002:
                xitong_data_refresh.setRefreshing(false);
                xitong_data_refresh.setVisibility(View.VISIBLE);
                nulldata_refreshsystem.setVisibility(View.GONE);
                if(adapter_xitong==null){
                     adapter_xitong = new LWNAdapter_messageSystem(view_xitong.getContext(), R.layout.view_mc_xitong, array_Allxitong);
                    listView_xitong.setAdapter(adapter_xitong);
                }else{
                    adapter_xitong.notifyDataSetChanged();
                }




                break;
            case 0003:
                dongtai_data_refresh.setRefreshing(false);
                dongtai_data_refresh.setLoading(false);
               nulldata_refreshdongtai.setRefreshing(false);
                if(adapter_dongtai!=null){
                    adapter_dongtai.notifyDataSetChanged();
                }
//                dongtai_data_refresh.setVisibility(View.GONE);
//                nulldata_refreshdongtai.setVisibility(View.VISIBLE);

                break;
            case 0004:
                xitong_data_refresh.setRefreshing(false);
                xitong_data_refresh.setLoading(false);
                nulldata_refreshsystem.setRefreshing(false);
                if(adapter_xitong!=null){
                    adapter_xitong.notifyDataSetChanged();
                }


                break;


        }
    }
};

    /**
     * 获取问题动态数据
     */
    public void getDongtaiMessageFromNetwork() {
        LWNNetworkCenter center = LWNNetworkCenter.getInstance();
        if(!center.checkNetworkState(activity_self)){
            ToastView_custom toast = new ToastView_custom(activity_self);
            toast.showCustom(activity_self, "检查网络,稍后重试");
            dongtai_data_refresh.setRefreshing(false);
            dongtai_data_refresh.setLoading(false);
            return;
        }
        String string_url = center.getInstance().getDongtaiMessage(start+"", "10", activity_self);
        HttpUtils http = center.getUtils();
        http.configDefaultHttpCacheExpiry(0*0);
        http.send(HttpRequest.HttpMethod.GET,
                string_url,
                new RequestCallBack<Object>() {
                    @Override
                    public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
//                        proDialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                            String string_code = jsonObject.getString("code");
                            String string_msg=jsonObject.getString("msg");
                            //System.out.println(string_code);
                            switch (string_code) {
                                case "10000": {
                                    JSONArray json_array = jsonObject.getJSONArray("data");
                                    array_dongtai = LWNJsonToData.getDongtaiWithJsonString(json_array);
                                    if (start == 0) {
                                        if (array_Alldongtai != null) {
                                            array_Alldongtai.clear();
                                        }
                                        array_Alldongtai.addAll(array_dongtai);
                                        handler.sendEmptyMessage(0001);
                                    } else {
                                        array_Alldongtai.addAll(array_dongtai);
                                        handler.sendEmptyMessage(0003);
                                    }

                                }
                                break;
                                case "10001": {
                                    dongtai_data_refresh.setRefreshing(false);
                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                            string_msg, Toast.LENGTH_SHORT);
                                    toast.setGravity(Gravity.CENTER, 0, 0);
                                    LinearLayout toastView = (LinearLayout) toast.getView();
                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                    imageCodeProject.setImageResource(R.drawable.error);
                                    toastView.addView(imageCodeProject, 0);
                                    toast.show();
                                }
                                break;
                                case "10003": {
                                    dongtai_data_refresh.setRefreshing(false);
                                    String token = LWNModel_user.getInstance().getString_token(activity_self);
                                    if (token != null) {
                                        Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                                string_msg, Toast.LENGTH_SHORT);
                                        toast.setGravity(Gravity.CENTER, 0, 0);
                                        LinearLayout toastView = (LinearLayout) toast.getView();
                                        ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                        imageCodeProject.setImageResource(R.drawable.error);
                                        toastView.addView(imageCodeProject, 0);
                                        toast.show();
                                        Intent intent = new Intent();
                                        intent.putExtra("name", "LeiPei");
                                        intent.setClass(activity_self, LWNLoginActivity.class);
                                        startActivity(intent);
                                    }
                                }
                                break;
                                case "10009":
                                {
                                    dongtai_data_refresh.setRefreshing(false);
                                    dongtai_data_refresh.setLoading(false);
                                    JSONArray json_array = jsonObject.optJSONArray("data");
                                    if(start==0&&json_array==null){
                                        nulldata_refreshdongtai.setRefreshing(false);
//                                        ToastView_custom toast = new ToastView_custom(activity_self);
//                                        toast.showCustom(activity_self, getResources().getDrawable(R.drawable.error), string_msg);
                                        if (array_Alldongtai != null) {//没有更多
                                            array_Alldongtai.clear();
                                           // handler.sendEmptyMessage(0003);
                                            dongtai_data_refresh.setVisibility(View.GONE);
                                            nulldata_refreshdongtai.setVisibility(View.VISIBLE);
                                        }
                                    }else{//没有数据
                                         handler.sendEmptyMessage(0003);
                                        ToastView_custom toast = new ToastView_custom(activity_self);
                                        toast.showCustom(activity_self,  string_msg);
                                    }

                                }
                                break;

                                default: {
                                    dongtai_data_refresh.setRefreshing(false);
                                    dongtai_data_refresh.setLoading(false);
                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                            string_msg, Toast.LENGTH_SHORT);
                                    toast.setGravity(Gravity.CENTER, 0, 0);
                                    LinearLayout toastView = (LinearLayout) toast.getView();
                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                    imageCodeProject.setImageResource(R.drawable.error);
                                    toastView.addView(imageCodeProject, 0);
                                    toast.show();
                                }
                                break;
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
//                            proDialog.dismiss();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
//                        proDialog.dismiss();
                        dongtai_data_refresh.setRefreshing(false);
                        dongtai_data_refresh.setLoading(false);
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }
                }
        );
    }

    /**
     * 获取系统通知
     */

    public void getSystemMessageFromNetwork() {
        LWNNetworkCenter center = LWNNetworkCenter.getInstance();
        if(!center.checkNetworkState(activity_self)){
            ToastView_custom toast = new ToastView_custom(activity_self);
            toast.showCustom(activity_self, "检查网络,稍后重试");
            xitong_data_refresh.setRefreshing(false);
            xitong_data_refresh.setLoading(false);
            return;
        }
        String string_url = center.getInstance().getSystemMessage(start+"", "6", activity_self);
        HttpUtils http = center.getUtils();
        http.configDefaultHttpCacheExpiry(0*0);
        http.send(HttpRequest.HttpMethod.GET,
                string_url,
                new RequestCallBack<Object>() {
                    @Override
                    public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
//                        proDialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                            String string_code = jsonObject.getString("code");
                            String string_msg=jsonObject.getString("msg");
                            switch (string_code) {
                                case "10000": {
                                    JSONArray json_array = jsonObject.getJSONArray("data");

                                    array_xitong = LWNJsonToData.getSystemWithJsonString(json_array);

                                    if(start==0){
                                        if(array_Allxitong!=null&array_Allxitong.size()!=0){
                                            array_Allxitong.clear();
                                        }
                                        array_Allxitong.addAll(array_xitong);
                                        handler.sendEmptyMessage(0002);
                                    }else{
                                        array_Allxitong.addAll(array_xitong);
                                        handler.sendEmptyMessage(0004);
                                    }

                                }
                                break;
                                case "10001": {
                                    xitong_data_refresh.setRefreshing(false);
                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                            string_msg, Toast.LENGTH_SHORT);
                                    toast.setGravity(Gravity.CENTER, 0, 0);
                                    LinearLayout toastView = (LinearLayout) toast.getView();
                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                    imageCodeProject.setImageResource(R.drawable.error);
                                    toastView.addView(imageCodeProject, 0);
                                    toast.show();
                                }
                                break;
                                case "10003": {
                                    xitong_data_refresh.setRefreshing(false);
                                    String token = LWNModel_user.getInstance().getString_token(activity_self);
                                    if (token != null) {
                                        Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                                string_msg, Toast.LENGTH_SHORT);
                                        toast.setGravity(Gravity.CENTER, 0, 0);
                                        LinearLayout toastView = (LinearLayout) toast.getView();
                                        ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                        imageCodeProject.setImageResource(R.drawable.error);
                                        toastView.addView(imageCodeProject, 0);
                                        toast.show();

                                        Intent intent = new Intent();
                                        intent.putExtra("name", "LeiPei");
                                        intent.setClass(activity_self, LWNLoginActivity.class);
                                        startActivity(intent);
                                        activity_self.finish();

                                    }
                                }
                                case "10009"://没有数据//没有更多
                                {
                                    xitong_data_refresh.setRefreshing(false);
                                    xitong_data_refresh.setLoading(false);
                                    JSONArray json_array = jsonObject.optJSONArray("data");
                                    if(start==0&&json_array==null){
                                        if(array_Allxitong!=null&array_Allxitong.size()!=0){
                                            array_Allxitong.clear();
                                        }
                                        xitong_data_refresh.setVisibility(View.GONE);
                                        nulldata_refreshsystem.setVisibility(View.VISIBLE);
                                        nulldata_refreshsystem.setRefreshing(false);
                                    }else{
                                        handler.sendEmptyMessage(0004);
                                        ToastView_custom toast = new ToastView_custom(activity_self);
                                        toast.showCustom(activity_self,  string_msg);
                                    }

                                }
                                break;
                                default: {
                                    xitong_data_refresh.setRefreshing(false);
                                    xitong_data_refresh.setLoading(false);
                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                            string_msg, Toast.LENGTH_SHORT);
                                    toast.setGravity(Gravity.CENTER, 0, 0);
                                    LinearLayout toastView = (LinearLayout) toast.getView();
                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                    imageCodeProject.setImageResource(R.drawable.error);
                                    toastView.addView(imageCodeProject, 0);
                                    toast.show();
                                }
                                break;
                            }

                        } catch (Exception e) {
                            //e.printStackTrace();
//                            proDialog.dismiss();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
//                        proDialog.dismiss();
                        xitong_data_refresh.setRefreshing(false);
                        xitong_data_refresh.setLoading(false);
                   }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }
                }
        );
    }

    /**
     * 更新通知未读数量
     */
    public void uploadQuestionMessageFromNetwork(String string_url) {
        if(!center.checkNetworkState(activity_self)){
            ToastView_custom toast = new ToastView_custom(activity_self);
            toast.showCustom(activity_self,  "检查网络,稍后重试");
            return;
        }
        HttpUtils http = center.getUtils();
        http.send(HttpRequest.HttpMethod.GET,
                string_url,
                new RequestCallBack<Object>() {
                    @Override
                    public void onSuccess(ResponseInfo<Object> objectResponseInfo) {try{
                        JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                        String string_code = jsonObject.getString("code");
                        String string_msg=jsonObject.getString("msg");
                    switch (string_code) {
                        case "10000":{

                        }

                            break;


                        case "10001": {

                        }
                        break;
                        case "10003": {
//                            String token = LWNModel_user.getInstance().getString_token(activity_self);
//                            if (token != null) {
//                                Toast toast = Toast.makeText(activity_self.getApplicationContext(),
//                                        string_msg, Toast.LENGTH_SHORT);
//                                toast.setGravity(Gravity.CENTER, 0, 0);
//                                LinearLayout toastView = (LinearLayout) toast.getView();
//                                ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
//                                imageCodeProject.setImageResource(R.drawable.error);
//                                toastView.addView(imageCodeProject, 0);
//                                toast.show();
//
                            Intent intent = new Intent();
                            intent.putExtra("name", "LeiPei");
                            intent.setClass(activity_self, LWNLoginActivity.class);
                            startActivity(intent);
                            activity_self.finish();
                          //  }
                        }
                        break;
                        default: {
//                            Toast toast = Toast.makeText(activity_self.getApplicationContext(),
//                                    string_msg, Toast.LENGTH_SHORT);
//                            toast.setGravity(Gravity.CENTER, 0, 0);
//                            LinearLayout toastView = (LinearLayout) toast.getView();
//                            ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
//                            imageCodeProject.setImageResource(R.drawable.error);
//                            toastView.addView(imageCodeProject, 0);
//                            toast.show();
                        }
                        break;

                    }

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {

                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }
                }
        );
    }



    ///清空系统列表
    public void cleanUnreadFromNetwork( final int type){
      //  final ProgressDialog proDialog = android.app.ProgressDialog.show(activity_self, "", "清除中，请稍候...");
        LWNNetworkCenter center = LWNNetworkCenter.getInstance();
        if(!center.checkNetworkState(activity_self)){
            ToastView_custom toast = new ToastView_custom(activity_self);
            toast.showCustom(activity_self,"检查网络,稍后重试");
            return;
        }
        final ProgressDialog proDialog = android.app.ProgressDialog.show(activity_self, "", "清除中，请稍候...");
        String string_url = center.getInstance().cleanUnreadCount(type+"", activity_self);
        HttpUtils http = center.getUtils();
        http.send(HttpRequest.HttpMethod.GET,
                string_url,
                new RequestCallBack<Object>() {
                    @Override
                    public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                        proDialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                            String string_code = jsonObject.getString("code");
                            String string_msg=jsonObject.getString("msg");

                            switch (string_code) {
                                case "10000": {
                                    ToastView_custom toast = new ToastView_custom(activity_self);
                                    toast.showCustom(activity_self,  string_msg);
                                    start=0;
                                    if(type==0){

                                        getDongtaiMessageFromNetwork();
                                    }else{
                                        getSystemMessageFromNetwork();
                                    }

                                }
                                break;
                                case "10001": {
//                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
//                                            string_msg, Toast.LENGTH_SHORT);
//                                    toast.setGravity(Gravity.CENTER, 0, 0);
//                                    LinearLayout toastView = (LinearLayout) toast.getView();
//                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
//                                    imageCodeProject.setImageResource(R.drawable.error);
//                                    toastView.addView(imageCodeProject, 0);
//                                    toast.show();
                                    ToastView_custom toast = new ToastView_custom(activity_self);
                                    toast.showCustom(activity_self, string_msg);
                                }
                                break;
                                case "10003": {
                                    String token = LWNModel_user.getInstance().getString_token(activity_self);
                                    if (token != null) {
                                        Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                                string_msg, Toast.LENGTH_SHORT);
                                        toast.setGravity(Gravity.CENTER, 0, 0);
                                        LinearLayout toastView = (LinearLayout) toast.getView();
                                        ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                        imageCodeProject.setImageResource(R.drawable.error);
                                        toastView.addView(imageCodeProject, 0);
                                        toast.show();

                                        Intent intent = new Intent();
                                        intent.putExtra("name", "LeiPei");
                                        intent.setClass(activity_self, LWNLoginActivity.class);
                                        activity_self.finish();
                                    }

                                }
                                break;
                                default: {
                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                            string_msg, Toast.LENGTH_SHORT);
                                    toast.setGravity(Gravity.CENTER, 0, 0);
                                    LinearLayout toastView = (LinearLayout) toast.getView();
                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                    imageCodeProject.setImageResource(R.drawable.error);
                                    toastView.addView(imageCodeProject, 0);
                                    toast.show();
                                }
                                break;
                            }

                        } catch (Exception e) {
                            //e.printStackTrace();
                            proDialog.dismiss();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        proDialog.dismiss();
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }
                }
        );
    }


    /**
     * 删除消息id
     * @param n_id
     */
    public void deletMessage( String n_id){
        isShow=false;
        final ProgressDialog proDialog = android.app.ProgressDialog.show(activity_self, "", "清除中，请稍候...");
        LWNNetworkCenter center = LWNNetworkCenter.getInstance();
        String string_url = center.getInstance().deletMessage(n_id, activity_self);
        HttpUtils http = center.getUtils();
        http.send(HttpRequest.HttpMethod.GET,
                string_url,
                new RequestCallBack<Object>() {
                    @Override
                    public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                        proDialog.dismiss();
                        try {
                            JSONObject jsonObject = new JSONObject(objectResponseInfo.result.toString());
                            String string_code = jsonObject.getString("code");
                            String string_msg=jsonObject.getString("msg");

                            switch (string_code) {
                                case "10000": {
                                    if(type==0){
                                        getDongtaiMessageFromNetwork();
                                    }else{
                                        getSystemMessageFromNetwork();
                                    }
                                }
                                break;
                                case "10001": {
                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                            string_msg, Toast.LENGTH_SHORT);
                                    toast.setGravity(Gravity.CENTER, 0, 0);
                                    LinearLayout toastView = (LinearLayout) toast.getView();
                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                    imageCodeProject.setImageResource(R.drawable.error);
                                    toastView.addView(imageCodeProject, 0);
                                    toast.show();
                                }
                                break;
                                case "10003": {
                                    String token = LWNModel_user.getInstance().getString_token(activity_self);
                                    if (token != null) {
                                        Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                                string_msg, Toast.LENGTH_SHORT);
                                        toast.setGravity(Gravity.CENTER, 0, 0);
                                        LinearLayout toastView = (LinearLayout) toast.getView();
                                        ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                        imageCodeProject.setImageResource(R.drawable.error);
                                        toastView.addView(imageCodeProject, 0);
                                        toast.show();

                                        Intent intent = new Intent();
                                        intent.putExtra("name", "LeiPei");
                                        intent.setClass(activity_self, LWNLoginActivity.class);}

                                }
                                break;
                                default: {
                                    Toast toast = Toast.makeText(activity_self.getApplicationContext(),
                                            string_msg, Toast.LENGTH_SHORT);
                                    toast.setGravity(Gravity.CENTER, 0, 0);
                                    LinearLayout toastView = (LinearLayout) toast.getView();
                                    ImageView imageCodeProject = new ImageView(activity_self.getApplicationContext());
                                    imageCodeProject.setImageResource(R.drawable.error);
                                    toastView.addView(imageCodeProject, 0);
                                    toast.show();
                                }
                                break;
                            }

                        } catch (Exception e) {
                            //e.printStackTrace();
                            proDialog.dismiss();
                        }

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        proDialog.dismiss();
                    }

                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }
                }
        );
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
        start=0;
        if(type==0){
            getDongtaiMessageFromNetwork();
        }else if(type==1){
            getSystemMessageFromNetwork();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }
}
